package QC.MessageBox.beans;

import QC.MessageBox.annotation.APPTable;
import QC.MessageBox.common.EmojiUtil;
import io.vertx.core.json.JsonObject;

/**
 * Created by Andaren on 2017/5/3.
 * Used for: 消息记录
 *  注意： 消息从微信到app 则：fromUser = openid, toUser = 客服ID
 *         消息从app到微信 则：fromUser = 客服ID, toUser = openid
 */
@APPTable(table = "t_msg_record")
public class MsgRecord {

    public static final String MSG_FROM_WX = "0";
    public static final String MSG_FROM_APP = "1";

    @APPTable(column = "id", isId = true)
    private Long id;            // 消息表主键

    @APPTable(column = "source_type")
    private String sourceType;  // 消息类型（0：来自微信 1：来自app）

    @APPTable(column = "openid")
    private String openId;      // 该消息对应的微信用户

    @APPTable(column = "csid")
    private String csId;        // 客服id

    @APPTable(column = "cs_nickname")
    private String csNickName;  // 客服昵称

    @APPTable(column = "msg_type")
    private String msgType;     // 消息类型

    @APPTable(column = "content")
    private String content;     // 消息

    @APPTable(column = "sendtime")
    private Long sendtime;      // 消息的发送时间

    @APPTable(column = "isread")
    private String isRead;      // 消息是否已读（0：未读，1：已读）

    public Long getId() {
        return id;
    }

    public MsgRecord setId(Long id) {
        this.id = id;
        return this;
    }

    public String getSourceType() {
        return sourceType;
    }

    public MsgRecord setSourceType(String sourceType) {
        this.sourceType = sourceType;
        return this;
    }

    public String getOpenId() {
        return openId;
    }

    public MsgRecord setOpenId(String openId) {
        this.openId = openId;
        return this;
    }

    public String getMsgType() {
        return msgType;
    }

    public MsgRecord setMsgType(String msgType) {
        this.msgType = msgType;
        return this;
    }

    public String getContent() {
        return content;
    }

    public MsgRecord setContent(String content) {
        this.content = content;
        return this;
    }

    public Long getSendtime() {
        return sendtime;
    }

    public MsgRecord setSendtime(Long sendtime) {
        this.sendtime = sendtime;
        return this;
    }

    public String getIsRead() {
        return isRead;
    }

    public MsgRecord setIsRead(String isRead) {
        this.isRead = isRead;
        return this;
    }

    public String getCsId() {
        return csId;
    }

    public MsgRecord setCsId(String csId) {
        this.csId = csId;
        return this;
    }

    public String getCsNickName() {
        return csNickName;
    }

    public MsgRecord setCsNickName(String csNickName) {
        this.csNickName = csNickName;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("MsgRecord{");
        sb.append("id=").append(id);
        sb.append(", sourceType='").append(sourceType).append('\'');
        sb.append(", openId='").append(openId).append('\'');
        sb.append(", csId='").append(csId).append('\'');
        sb.append(", csNickName='").append(csNickName).append('\'');
        sb.append(", msgType='").append(msgType).append('\'');
        sb.append(", content='").append(content).append('\'');
        sb.append(", sendtime='").append(sendtime).append('\'');
        sb.append(", isRead='").append(isRead).append('\'');
        sb.append('}');
        return sb.toString();
    }

    /**
     * 过滤emoji表情（替换成*）
     * [将emoji编码]
     * @return
     */
    public MsgRecord dropEmoji() {
        /**
         * 因为MySQL数据库中需要存emoji表情的表项已经设置了utf8mb4_unicode_ci
         * 所以不需要丢弃表情了(然而并没有)
         */
//        this.content = EmojiUtil.dropEmojiInMsg(this.content);
        encodeContent();
        return this;
    }

    public String contentByType() {
        JsonObject contentJson = new JsonObject(content);
        String realContent = "";
        if ("text".equals(msgType)) {
            realContent = contentJson.getString("content");
        } else if ("image".equals(msgType)){
            realContent = contentJson.getString("imageUri");
        }
        return realContent;
    }

    public MsgRecord encodeContent() {
        JsonObject contentJson = new JsonObject(this.content);
        String encodedContent = EmojiUtil.encryptEmoji(contentJson.getString("content"));
        contentJson.put("content", encodedContent);
        this.content = contentJson.encode();
        return this;
    }

    public MsgRecord decodeContent() {
        JsonObject contentJson = new JsonObject(this.content);
        String decodedContent = EmojiUtil.decodeEmoji(contentJson.getString("content"));
        contentJson.put("content", decodedContent);
        this.content = contentJson.encode();
        return this;
    }

}
